<!-- 
说明：物流配置表表单
作者：292303959@qq.com
日期：2018-11-2 15:57:54
版本：1.0.0
-->
<template>
  <el-dialog :title="title" append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="600px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="ui.loadingText"
             v-loading="ui.busying">
        <el-row type="flex">
            <el-col :span="24">
                <mk-form-item label="物流配置编码"
                                prop="lccode"
                                :mk-le-length="32"
                                required>
                    <el-input v-model="form.lccode"></el-input>
                </mk-form-item>
            </el-col>
        </el-row>
    
        <el-row type="flex">
            <el-col :span="24">
                <mk-form-item label="物流配置值"
                                prop="lcvalue"
                                :mk-le-length="200"
                                required>
                    <el-input v-model="form.lcvalue"></el-input>
                </mk-form-item>
            </el-col>
        </el-row>
        
    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './form-data'
import cloneDeep from 'lodash.clonedeep'
export default {
  name: 'cp-logistics-config-form',
  mixins: [MkBase],
  props: {
    psid: String,
    isAdd: Boolean,
    title: String,
    btnName: String,
    row: Object
  },
  async mounted () {
    if (this.isAdd === false) {
      this.form = cloneDeep(this.row)
    }
    this.ui.busying = false
  },
  data () {
    const form = formData()
    return {
      ui: {
        formLabelWidth: '100px',
        busying: true,
        loadingText: '正在加载'
      },
      form
    }
  },
  methods: {
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.ui.loadingText = '正在保存'
        this.ui.busying = true
        try {
          let data = await this[this.isAdd === false ? '$put' : '$post'](
            `/cpLogisticsConfig/${
              this.isAdd === false ? 'update' : 'add'
            }.json`,
            {
              cpLogisticsConfig: this.form
            }
          )
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.ui.busying = false
        }
      }
    },
    handleClose () {
      this.$emit('close')
    }
  }
}
</script>
